package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.application.annotation.DataScope;
import com.wlyuan.open.scanner.dao.entity.GoodsDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-09-23
 */
public interface GoodsMapper extends BaseMapper<GoodsDO> {

    String QUERY_ID_SQL = "SELECT tg.Goods_ID FROM TMS_MGoods tg ";

    String QUERY_SQL = "SELECT tg.Goods_ID,tg.Goods_GUID,tg.opt_status,tg.Goods_Code," +
            "tg.Goods_Name,tg.Goods_TypeID,tg.Goods_CustomerCompanyID," +
            "tg.Goods_Price,tg.Goods_SPC,tg.Goods_Volume,tg.Goods_Long," +
            "tg.Goods_Width,tg.Goods_Height,tg.Goods_Weight,tg.Goods_MWeight," +
            "tg.Goods_Creator,tg.Goods_CreatorCompanyID as goodsCreatorcompanyid,tg.Goods_InsertTime," +
            "tg.Goods_Updater,tg.Goods_UpdateTime,tg.Goods_Invalid,tg.Goods_Comments," +
            "tg.Goods_Unit,tg.Goods_BarCode,tg.Goods_BoxNo,tg.Goods_ShortName," +
            "tg.Goods_FromOperator,tg.Goods_TerminalGoodsID,tg.Goods_TerminalGoodsCode," +
            "tg.Goods_EnName,tg.Goods_HotCount,tg.Goods_Amount,tg.Goods_CustomerID" +
            ",ISNULL(tm.Customer_Name,'') as customerName,ISNULL(tg.Goods_Unit,0) as unitId, ISNULL(tg.Goods_TypeID,0) as typeId " +
            ",ISNULL(pu.Unit_Name,'') as unit,tmt.GoodsType_Name as type,tm.Customer_OwnerCompany as customerCompanyId " +
            "FROM TMS_MGoods tg ";


    String LEFT_JOIN = "left join TMS_MCustomer tm on tg.Goods_CustomerID = tm.Customer_ID " +
            "left join Price_Unit pu on tg.Goods_Unit = pu.Unit_ID " +
            "left join TMS_MGoodsType tmt on tg.Goods_TypeID = tmt.GoodsType_ID ";

    String WHERE = "WHERE tg.Goods_CreatorCompanyID > 0 and tg.Goods_CustomerID > 0 ";

    String ORDER_BY = " ORDER BY tg.Goods_ID";

    /**
     * 获取物品id集合
     *
     * @return
     */
    @Select(QUERY_ID_SQL + LEFT_JOIN + WHERE + ORDER_BY + " OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "Goods_CreatorCompanyID", alias = "tg")
    List<Long> getGoodsIds(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    @Select("SELECT count(*) FROM TMS_MGoods tg " + LEFT_JOIN + WHERE)
    Integer count();

    @Select("SELECT count(*) FROM TMS_MGoods tg " + LEFT_JOIN + WHERE + " and tg.Goods_CreatorCompanyID = #{tenantId}")
    Integer countByTenantId(@Param("tenantId") Long tenantId);

    @Select(QUERY_ID_SQL + LEFT_JOIN + WHERE + " and tg.Goods_CreatorCompanyID = #{tenantId}" + ORDER_BY + " OFFSET ( #{pageNum} -1)* #{pageSize} ROWS FETCH next #{pageSize} rows only")
    @DataScope(column = "Goods_CreatorCompanyID", alias = "tg")
    List<Long> getGoodsIdsByTenantId(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("tenantId") Long tenantId);

    /**
     * 物品列表
     *
     * @return
     */
    @Select(QUERY_SQL + LEFT_JOIN + WHERE + ORDER_BY)
    @DataScope(column = "Goods_CreatorCompanyID", alias = "tg")
    List<GoodsDO> list();

    /**
     * 根据ID获取物品
     *
     * @param goodsId
     * @return
     */
    @Select(QUERY_SQL + LEFT_JOIN + " WHERE tg.Goods_ID = #{goodsId}")
    GoodsDO getById(@Param("goodsId") Long goodsId);

    @Select(QUERY_ID_SQL + " WHERE tg.Goods_InsertTime >= #{startTime} and tg.Goods_InsertTime <= #{endTime}")
    List<Long> getByTime(@Param("startTime") String startTime, @Param("endTime") String endTime);

    @Select(QUERY_ID_SQL + " WHERE tg.Goods_CreatorCompanyID = #{tenantId} and tg.Goods_InsertTime >= #{startTime} and tg.Goods_InsertTime <= #{endTime}")
    List<Long> getByTenantIdTime(@Param("tenantId") Long tenantId, @Param("startTime") String startTime, @Param("endTime") String endTime);
}
